<?php

$newId = $_GET['id'];
$conn=new mysqli("localhost","root","123456","htmlkeshe");

//删除txt
$sql0="SELECT * FROM news WHERE newsId=".$newId.";";
$result0=$conn->query($sql0) or die(mysqli_error($conn));
if($result0->num_rows>0) {
    while ($row = $result0->fetch_assoc()) {
        $txturl=$row['newsContent'];
        $authorId = $row['authorId'];
    }
}
unlink($txturl);

//删除图片
$url="./resource/news/news".$newId;
$picurl=$url."/news".$newId.".jpeg";
unlink($picurl);

//删除文件夹
rmdir($url);


$sql="SELECT * from user where id=".$authorId;
$result=$conn->query($sql) or die(mysqli_error($conn));;
$row = $result->fetch_assoc();
$num = $row["nrOfNews"]-1;
$sql = "UPDATE user set nrOfNews=$num WHERE id=".$authorId;
$conn->query($sql);

//删除数据库中的数据
$sql="DELETE FROM news where newsId = ".$newId;
$result=$conn->query($sql);
$sql="delete from comment where newsId = ".$newId;
$conn->query($sql);
$conn->close();
header('Refresh: 0; url=Personal.php');
//echo "<a href='Personal.php'>正在跳转到个人中心,若长时间未响应,则点击此处</a>";
?>
<script>
    alert("删除成功!")
</script>
